home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / netlib / RCS / Net_StringToInetAddr.c,v < prev    next >
Encoding:
Text File  |  1992-06-10  |  3.4 KB  |  168 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     88.11.21.09.10.29;  author mendel;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Formed from net.c of src/lib/old/net.c.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* 
  27.  * Net_StringToInetAddr.c --
  28.  *
  29.  *    Convert a string to an internet address.
  30.  *
  31.  * Copyright 1987 Regents of the University of California
  32.  * All rights reserved.
  33.  * Permission to use, copy, modify, and distribute this
  34.  * software and its documentation for any purpose and without
  35.  * fee is hereby granted, provided that the above copyright
  36.  * notice appear in all copies.  The University of California
  37.  * makes no representations about the suitability of this
  38.  * software for any purpose.  It is provided "as is" without
  39.  * express or implied warranty.
  40.  */
  41.  
  42. #ifndef lint
  43. static char rcsid[] = "$Header: net.c,v 2.0 87/08/11 09:34:20 brent Exp $ SPRITE (Berkeley)";
  44. #endif not lint
  45.  
  46.  
  47. #include "sprite.h"
  48. #include "net.h"
  49.  
  50. /*
  51.  *----------------------------------------------------------------------
  52.  *
  53.  * Net_StringToInetAddr --
  54.  *
  55.  *    This routine takes a string form of an Internet address and
  56.  *    converts it to the Net_InetAddress form. The string must be
  57.  *    null-terminated.
  58.  *
  59.  *    "All the network library routines call this routine to interpret 
  60.  *    entries in the data bases which are expected to be an address.
  61.  *     The value returned is in network order."
  62.  *
  63.  * Results:
  64.  *    The Internet address in the Net_InetAddress form.
  65.  *
  66.  * Side effects:
  67.  *    None.
  68.  *
  69.  *----------------------------------------------------------------------
  70.  */
  71.  
  72. Net_InetAddress
  73. Net_StringToInetAddr(cp)
  74.     register char *cp;
  75. {
  76.     register unsigned int value;
  77.     register unsigned int base;
  78.     register char c;
  79.     unsigned int parts[4];
  80.     register unsigned int *partsPtr = parts;
  81.  
  82. again:
  83.     /*
  84.      * Collect number up to ``.''.
  85.      * Values are specified as for C: 0x=hex, 0=octal, other=decimal.
  86.      */
  87.  
  88.     value = 0; 
  89.     base = 10;
  90.     if (*cp == '0') {
  91.     base = 8;
  92.     cp++;
  93.     }
  94.     if (*cp == 'x' || *cp == 'X') {
  95.     base = 16;
  96.     cp++;
  97.     }
  98.  
  99.     c = *cp;
  100.     while (c != '\0') {
  101.     if (isdigit(c)) {
  102.         value = (value * base) + (c - '0');
  103.     } else if (base == 16 && isxdigit(c)) {
  104.         value = (value << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
  105.     } else {
  106.         break;
  107.     }
  108.     cp++;
  109.     c = *cp;
  110.     }
  111.  
  112.     if (*cp == '.') {
  113.     /*
  114.      * Internet format:
  115.      *    a.b.c.d
  116.      *    a.b.c    (with c treated as 16-bits)
  117.      *    a.b    (with b treated as 24 bits)
  118.      */
  119.     if (partsPtr >= parts + 4) {
  120.         return(NET_INET_BROADCAST_ADDR);
  121.     }
  122.     *partsPtr = value;
  123.     partsPtr++;
  124.     cp++;
  125.     goto again;
  126.     }
  127.  
  128.     /*
  129.      * Check for trailing characters.
  130.      */
  131.     if ((*cp != '\0') && (!isspace(*cp))) {
  132.     return(NET_INET_BROADCAST_ADDR);
  133.     }
  134.     *partsPtr = value;
  135.     partsPtr++;
  136.  
  137.     /*
  138.      * Concoct the address according to the number of parts specified.
  139.      */
  140.  
  141.     switch (partsPtr - parts) {
  142.  
  143.     case 1:                /* a -- 32 bits */
  144.         value = parts[0];
  145.         break;
  146.  
  147.     case 2:                /* a.b -- 8.24 bits */
  148.         value = (parts[0] << 24) | (parts[1] & 0xffffff);
  149.         break;
  150.  
  151.     case 3:                /* a.b.c -- 8.8.16 bits */
  152.         value = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  153.             (parts[2] & 0xffff);
  154.         break;
  155.  
  156.     case 4:                /* a.b.c.d -- 8.8.8.8 bits */
  157.         value = (parts[0] << 24) | ((parts[1] & 0xff) << 16) |
  158.               ((parts[2] & 0xff) << 8) | (parts[3] & 0xff);
  159.         break;
  160.  
  161.     default:
  162.         return(NET_INET_BROADCAST_ADDR);
  163.     }
  164.     return(Net_HostToNetInt(value));
  165. }
  166.  
  167. @
  168.